package com.renwei.yunlong.activity.me;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.contacts.CompanySendInvitationActivity;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.InvitationCompanyBean;
import com.renwei.yunlong.bean.InvitationCompanyJsonBean;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.view.CircleTextImage;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangqi on 2017/10/10.
 */

public class InvitationCompanyActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.tv_search)
    TextView tvSearch;
    @Bind(R.id.recyclerView)
    XRecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private int page = 1;
    private RecyAdapter adapter;
    private String companyName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitation_company);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initView();
    }

    private void initView() {
        ivBack.setOnClickListener(this);
        tvSearch.setOnClickListener(this);
        adapter = new RecyAdapter(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                adapter.clean();
                initData();
                recyclerView.setLoadingMoreEnabled(true);
            }

            @Override
            public void onLoadMore() {
                page++;
                initData();
            }
        });
        stateLayout.showEmptyView();
    }


    private void initData() {
        HttpPresenter hp = new HttpPresenter(this);
        hp.setUrl(Api.$().MIDDLE_INVITATION_COMPANY);
        if ("1".equals(companyType)) {
            String json = new Gson().toJson(new InvitationCompanyJsonBean(2, 2, companyName, ownerBean.getRows().getCompany().getCompanyCode(),
                    page, 20));
            hp.putParam("jsonBean", json);
        } else if ("2".equals(companyType)) {
            String json = new Gson().toJson(new InvitationCompanyJsonBean(1, 2, companyName, serviceLoginBean.getRows().getCompany().getCompanyCode(),
                    page, 20));
            hp.putParam("jsonBean", json);
        }
        hp.setshowDialogcontent("正在加载");
        hp.setCallBack(new HttpTaskListener() {
            @Override
            public void onSuccess(int requestId, String result) {
                recyclerView.loadMoreComplete();
                recyclerView.refreshComplete();
                InvitationCompanyBean bean = new Gson().fromJson(result, InvitationCompanyBean.class);
                int code = bean.getMessage().getCode();
                if (code == 200) {
                    setData(bean.getRows());
                } else if ("2".equals(companyType)) {//服务商
                    switch (code) {
                        case 1008:
                            showCenterInfoMsg("无权限访问");
                            break;
                        case 1005:
                            showCenterInfoMsg("此公司已绑定");
                            break;
                        case 1025:
                            showCenterInfoMsg("对方已发过申请，请进行确认！");
                            break;
                        case 1026:
                            showCenterInfoMsg("已发过申请，请等待对方确认！");
                            break;
                        default:
                            showCenterInfoMsg(bean.getMessage().getMessage());
                            break;
                    }
                } else if ("1".equals(companyType)) {//业主
                    switch (code) {
                        case 1008:
                            showCenterInfoMsg("无权限访问");
                            break;
                        case 1005:
                            showCenterInfoMsg("此公司已绑定");
                            break;
                        case 1025:
                            showCenterInfoMsg("已发过申请，请等待对方确认！");
                            break;
                        case 1026:
                            showCenterInfoMsg("对方已发过申请，请进行确认");
                            break;
                        default:
                            showCenterInfoMsg(bean.getMessage().getMessage());
                            break;
                    }
                }
            }

            @Override
            public void onException(int requestId, String code) {
                page--;
                stateLayout.showErrorView();
                recyclerView.loadMoreComplete();
                recyclerView.refreshComplete();
            }
        });
        hp.post();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_search:
                String s = etSearch.getText().toString().trim();
                if (TextUtils.isEmpty(s)) {
                    showCenterInfoMsg("请输入企业名称");
                } else {
                    companyName = s;
                    adapter.clean();
                    page = 1;
                    initData();
                }
                break;
            default:
                break;
        }
    }


    private void setData(List<InvitationCompanyBean.RowsBean> rows) {
        if (rows != null && rows.size() < 20) {
            recyclerView.setLoadingMoreEnabled(false);
        }
        adapter.addAll(rows);
        if (adapter.getData().size() == 0) {
            stateLayout.showSearchEmptView();
        } else {
            stateLayout.showContentView();
        }
    }

    class RecyAdapter extends BaseRecyclerViewAdapter<InvitationCompanyBean.RowsBean> {

        public RecyAdapter(Context context) {
            super(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.item_invitation_company, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            final InvitationCompanyBean.RowsBean bean = getItem(position);
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.tvName.setText(bean.getCompanyName());
            viewHolder.btInva.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 邀请
                    CompanySendInvitationActivity.OpenActivity(mContext, bean.getCompanyName(), bean.getCompanyCode());
                }
            });

            viewHolder.ivHead.setText4CircleImage(String.valueOf(bean.getCompanyName()));
            viewHolder.ivHead.setTextCircleColor(Color.parseColor("#3bbe7c"));
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_head)
        CircleTextImage ivHead;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.bt_inva)
        Button btInva;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static void OpenActivity(Context context) {
        context.startActivity(new Intent(context, InvitationCompanyActivity.class));
    }
}
